<?php
require_once "./interface/IController.php";
class StatisticController implements IController {
	private $view;
	
	public function __construct() {
		$this->view = new View ();
	}
	
	// 设置方法的访问权限，若权限不匹配，则调用 forbidAction()
	public function getAccessRule() {
		return array (UserBean::$_ADMIN => array ("to404", "coursequery","courseprint", "activityquery" ), UserBean::$_SENATE => array ("to404", "coursequery","courseprint", "activityquery" ), UserBean::$_INSTITUE => array ("to404", "coursequery","courseprint", "activityquery" ), UserBean::$_GUEST => array ("to404", "forbid" ) );
	}
	
	public function courseQueryAction() {
		// var_dump($_POST);
		$data=array();
		if (isset ( $_POST ['department'] ) && isset ( $_POST ['year'] )
		&& isset ( $_POST ['month'] ) && isset ( $_POST ['type']
		)) {
			$dep = $_POST ['department'];
			$year = $_POST ['year'];
			$month = $_POST ['month'];
			$techerType = $_POST ['type'];
// 			$dep = 'software';
// 			$year = '2012';
// 			$month = '2';
// 			$techerType = 1;
			$data['year']=$year;
			$data['month']=$month;
			$data['department']=$dep;
			$data['type'] = $techerType;
			$sqlTimeBegin = $year . '-' . $this->mon2str ( $month + 1 ); //零点
			$sqlTimeEnd = $year . '-' . $this->mon2str ( $month );
			if($techerType==1)
				$sql = "SELECT * FROM course WHERE courseBegin<'$sqlTimeBegin' and courseEnd>='$sqlTimeEnd' and department='$dep' and teacherType='$techerType'";
			else 
				$sql = "SELECT * FROM course WHERE courseBegin<'$sqlTimeBegin' and courseEnd>='$sqlTimeEnd' and department='$dep' and teacherType<>'1'";
			$db = MysqliObj::getInstance ();
			$dbData = $db->fetch_arrays ( $sql );
			
			$courseItemArray = array ();
			if (is_array ( $dbData )) {
				foreach ( $dbData as $d ) {
					$d ['checkTime'] = $sqlTimeEnd;
					$courseItem = new CourseItemBean ( $d );
					array_push ( $courseItemArray, $courseItem );
				}
			}
			$data['items']=$courseItemArray;
		}
		$this->view->render ( "statistic_course.php" , $data);
	}
	
	public function coursePrintAction(){
		if(isset($_GET['department'])&&isset($_GET['type'])&&isset($_GET['year'])&&isset($_GET['month'])){
			$_POST['type'] = $_GET['type'];
			$_POST['department'] = $_GET['department'];
			$_POST['year'] = $_GET['year'];
			$_POST['month'] = $_GET['month'];
			$data=$this->getCourseQueryData();
			$this->view->render("s_course_print.php",$data);
		}else{
			$data = false;
			$this->view->render("s_course_print.php",$data);
		}
		
	}
	
	public function activityQueryAction(){
		$data = array();
		
		if (isset ( $_POST ['beginTime'] ) && isset ( $_POST ['endTime'] )) {
			$beginTime = $_POST['beginTime'];
			$endTime = $_POST['endTime'];
			
			$sqlTimeBegin = date("Y-m-d",strtotime( $beginTime )); //plus one more day
			$sqlTimeEnd = date("Y-m-d", strtotime( $endTime )); //plus one more day
			
			if (isset ( $_POST ['page'] )) {
				$page = $_POST ['page'];
			} else {
				$page = 1;
			}
				
			$pageSize = Sysconfig::getPageSize ();
			$begin = ($page - 1) * $pageSize;
			
			$sql = "SELECT * FROM activity WHERE activityBegin<='$sqlTimeEnd' and activityEnd>='$sqlTimeBegin' ORDER BY id DESC limit $begin, $pageSize";
			$db = MysqliObj::getInstance ();
			$activityArray = array();
			$arrays = $db->fetch_arrays ( $sql );
			foreach ($arrays as $a)
				$activityArray[]=new ActivityBean($a);
			$data['beginTime']=$beginTime;
			$data['endTime']=$endTime;
			$data['items'] = $activityArray;
		}
		$this->view->render("statistic_activity.php" , $data);
	}
	public function activityPrintAction(){
		if(isset($_GET['beginTime'])&&isset($_GET['endTime'])){
			$_POST['beginTime'] = $_GET['beginTime'];
			$_POST['endTime'] = $_GET['endTime'];
			$data=$this->getActivityQuaryData();
			$this->view->render("s_activity_print.php",$data);
		}else{
			$data = false;
			$this->view->render("s_activity_print.php",$data);
		}
	
	}
	public function to404Action() {
		$this->view->render ( "404.php" );
	}
	
	// 当请求的 action 不合法时调用此方法
	// 重载 IController 接口
	public function forbidAction() {
		if ($_SESSION ['userinfo'] ['type'] == UserBean::$_GUEST) {
			redirectTo ( urlToAction ( 'user', 'login' ) );
		}
		$this->view->render ( "forbid.php" );
	}
	
	private function mon2str($month) {
		$month = $month+0;
		if (is_numeric ( $month )) {
			return $month < 10 ? '0' . $month : $month;
		}
	}
	private function getCourseQueryData(){
		$data=array();
		if (isset ( $_POST ['department'] ) && isset ( $_POST ['year'] )
				&& isset ( $_POST ['month'] ) && isset ( $_POST ['type']
				)) {
			$dep = $_POST ['department'];
			$year = $_POST ['year'];
			$month = $_POST ['month'];
			$techerType = $_POST ['type'];
			// 			$dep = 'software';
			// 			$year = '2012';
			// 			$month = '2';
			// 			$techerType = 1;
			$data['year']=$year;
			$data['month']=$month;
			$data['department']=$dep;
			$data['type'] = $techerType;
			$sqlTimeBegin = $year . '-' . $this->mon2str ( $month + 1 ); //零点
			$sqlTimeEnd = $year . '-' . $this->mon2str ( $month );
			if($techerType==1)
				$sql = "SELECT * FROM course WHERE courseBegin<'$sqlTimeBegin' and courseEnd>='$sqlTimeEnd' and department='$dep' and teacherType='$techerType'";
			else
				$sql = "SELECT * FROM course WHERE courseBegin<'$sqlTimeBegin' and courseEnd>='$sqlTimeEnd' and department='$dep' and teacherType<>'1'";
			$db = MysqliObj::getInstance ();
			$dbData = $db->fetch_arrays ( $sql );
				
			$courseItemArray = array ();
			if (is_array ( $dbData )) {
				foreach ( $dbData as $d ) {
					$d ['checkTime'] = $sqlTimeEnd;
					$courseItem = new CourseItemBean ( $d );
					array_push ( $courseItemArray, $courseItem );
				}
			}
			$data['items']=$courseItemArray;
			return $data;
		}
		return false;
	}
	private function getActivityQuaryData(){
		$data = array();
		
		if (isset ( $_POST ['beginTime'] ) && isset ( $_POST ['endTime'] )) {
			$beginTime = $_POST['beginTime'];
			$endTime = $_POST['endTime'];
				
			$sqlTimeBegin = date("Y-m-d",strtotime( $beginTime ) + 3600 * 24); //plus one more day
			$sqlTimeEnd = date("Y-m-d", strtotime( $endTime ) + 3600 * 24); //plus one more day
				
			if (isset ( $_POST ['page'] )) {
				$page = $_POST ['page'];
			} else {
				$page = 1;
			}
		
			$pageSize = Sysconfig::getPageSize ();
			$begin = ($page - 1) * $pageSize;
				
			$sql = "SELECT * FROM activity WHERE activityBegin<'$sqlTimeEnd' and activityEnd>'$sqlTimeBegin' ORDER BY id DESC limit $begin, $pageSize";
			$db = MysqliObj::getInstance ();
			$activityArray = array();
			$arrays = $db->fetch_arrays ( $sql );
			foreach ($arrays as $a)
				$activityArray[]=new ActivityBean($a);
			$data['beginTime']=$beginTime;
			$data['endTime']=$endTime;
			$data['items'] = $activityArray;
			return $data;
		}
		return false;
	}
}

?>